<?php require_once('../../Connections/cotiza.php'); ?>
<?php 
session_start();
$permisos = $_SESSION['permisos'];
$vendedor = $_SESSION['usuario'];

$order     = " ORDER BY ".$_GET['order']." ".$_GET['tp'];

$campos    = $_POST['campo'];
$criterios = $_POST['criterio'];
$patron1   = $_POST['patron1'];
$patron2   = $_POST['patron2'];

function crea_condicion($campo,$c,$pat1,$pat2)
{
	switch ($c) {
		case 1: return $campo." = '".$pat1."'";
			    //break;
		case 2: return $campo." <> '".$pat1."'";
			    //break;
		case 3: return $campo." LIKE '%".$pat1."%'";
			    //break;
		case 4: return $campo." <= '".$pat1."' AND ".$campo." >= '".$pat2."'";
			    //break;
		case 5: return $campo." LIKE '".$pat1."%'";
			    //break;
		case 6: return $campo." >= '".$pat1."'";
			    //break;
		case 7: return $campo." <= '".$pat1."'";
			    //break;
	}
}

if($order == " ORDER BY  " && ($patron1[0] != "" || $patron2[0] != ""))
{
	$i=0;
	foreach($campos as $cm)
	{
		$campo[$i] = $cm;
		$i++;
	}
	$i=0;
	foreach($criterios as $cr)
	{
		$criterio[$i] = $cr;
		$i++;
	}
	$i=0;
	foreach($patron1 as $pat1)
	{
		$p1[$i]	= $pat1;
		$i++;
	}
	$i=0;
	foreach($patron2 as $pat2)
	{
		$p2[$i]	= $pat2;
		$i++;
	}
	
	for($j=0;$j<$i;$j++)
	{
		if($j==0)
			$condicion = " AND ".crea_condicion($campo[$j],$criterio[$j],$p1[$j],$p2[$j]).$restriccion;
		else
			$condicion .= " AND ".crea_condicion($campo[$j],$criterio[$j],$p1[$j],$p2[$j]);
	}
}
?>
<?php
mysql_select_db($database_cotiza, $cotiza);

if($_GET['order'] == "")
{
	if($permisos == "1")
		$restriccion = " AND solic_user_c = '$vendedor' ";
		
	$query = "SELECT * FROM solic_view WHERE solic_status = 5";
	$query_solicitud = $query.$condicion.$restriccion." ORDER BY solic_id";
}
else
{
	$query = $_POST['consulta'];
	$query_solicitud = $query.$order;
}

$solicitud = mysql_query($query_solicitud, $cotiza) or die(mysql_error());
$row_solicitud = mysql_fetch_assoc($solicitud);
$totalRows_solicitud = mysql_num_rows($solicitud);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>...::: Browse Solicitud - Resulset:::...</title>
<link href="../../main.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function ordenar(campo)
{
	var tp = '<?php echo $_GET['tp'];?>';
	
	if(tp == "" || tp == "DESC")
		tp = "ASC";
	else
		tp = "DESC";
		
	document.forms.form1.action= "browsolicitud_resulset.php?order=" + campo + "&tp=" + tp; 
	document.forms.form1.submit();
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
  <table border="0" align="center">
    <tr bgcolor="#CCCCCC">
      <td width="86"><a href="javascript:ordenar('solic_id');"><strong>N&ordm; Solicitud</strong></a></td>
      <td width="70"> <a href="javascript:ordenar('status_desc');"><strong>Estado</strong></a></td>
      <td width="105"> <a href="javascript:ordenar('solic_fecha');"><strong>Fecha</strong></a></td>
      <td width="105"> <a href="javascript:ordenar('solic_fecha_solic');"><strong>Fecha Solicitada</strong></a></td>
      <td width="180"><a href="javascript:ordenar('cm_sort');"><strong>Cliente</strong></a></td>
      <td width="150"><a href="javascript:ordenar('nombre');"><strong>Vendedor</strong></a></td>
      <td width="170"><a href="javascript:ordenar('solic_npcliente');"><strong>Nombre del producto</strong></a></td>
      <td colspan="3"><strong>Opciones</strong></td>
    </tr>
    <?php $i=0;if($totalRows_solicitud > 0){do { $i++;?>
    <tr <?php if($i % 2 == 0) echo "bgcolor=\"#F4F4F4\""; ?>>
      <td><?php echo $row_solicitud['solic_id']; ?></td>
      <td><?php echo $row_solicitud['status_desc']; ?></td>
      <td><?php echo $row_solicitud['solic_fecha']; ?></td>
      <td><?php echo $row_solicitud['solic_fecha_solic']; ?></td>
      <td><?php echo $row_solicitud['cm_sort']; ?></td>
      <td><?php echo $row_solicitud['nombre']; ?></td>
      <td><?php echo substr($row_solicitud['solic_npcliente'],0,32); ?></td>
      <td width="16"><a href="../rep/repofertacom.php?id_solicitud=<?php echo $row_solicitud['solic_id'];?>" target="_parent"><img src="../img/coins.png" alt="Revisar Oferta Comercial" width="16" height="16" border="0" /></a></td>
      <td width="14"><a href="../rep/repov_2.php?id_s=<?php echo $row_solicitud['solic_id'];?>" target="_parent"><img src="../img/ov.png" alt="Revisar Orden de venta" width="16" height="16" border="0" /></a></td>
      <td width="15"><img src="../img/money.png" width="16" height="16" /></td>
    </tr>
    <?php } while ($row_solicitud = mysql_fetch_assoc($solicitud)); }?>
  </table>
  <input type="hidden" name="consulta" id="consulta" value="<?php echo $query.$condicion;?>" />
</form>
</body>
</html>
<?php
mysql_free_result($solicitud);
?>
<?php 
mysql_close($cotiza);
?>